﻿#pragma once

#include <AsThread>
#include <AsGlobalData>
#include <AsProcessCoreData>
#include <QVector>
#include <QMap>
#include <QMutex>
#include <QListWidgetItem>

#define ONCE_DATA_LENGTH 1000
#define ONE_MAT_ROW 1000
#define MAT_ROW 1000

class CoreData :
	public AsProcessCoreData
{
	Q_OBJECT
public:
	CoreData();
	~CoreData();

//算法函数
private:
	//图像算法
	cv::Mat normalizeToRange(const cv::Mat& inputMat);
	cv::Rect getBox(const cv::Mat& input, double thres);
	float getNormalvalue(const cv::Mat& inputMat, float input);
	cv::Mat getLocalMat(const cv::Mat&mat, float threshold, int edgeWidth);

	//排序算法
	int partition(QList<int> &arr, int low, int high);
	void quickSort(QList<int> &arr, int low, int high);

signals:
	void allowTimerStart();
	void getDatasOfThisHeightOver();

public slots:
	void handleDatas(int index);
	void extrastGirdleMat();
public:
	void initCoreData();
	void shiftTriggerStateByHardware();

	void setCurrentHeight(int height);
	int getCurrentHeight();

	void setMapListItem(int height, QListWidgetItem *item);
	QListWidgetItem* getListItemWithHeight(int height);
	int getHeightWithListItem(QListWidgetItem *item);
	QMap<int, QListWidgetItem*> getListItemMap();

	void setMatDataMap(int height, QVector<cv::Mat> matVec);
	//QVector<cv::Mat> getMatDataWithHeight(int height);
	QMap<int, QVector<cv::Mat>> getMatDatasMap();
	QMap<int, cv::Mat> getAverageMatMap();
	void insertAverageMatMap(int height,cv::Mat mat);
	QMap<int, QVector<QVector3D>> getVec3DMap();
	void insertVec3DMap(int height, QVector<QVector3D> vec3dVec);

	//void setOnceXAllDataMap(int height, QVector<double> onceAllXDatasVec);
	//void setOnceYAllDataMap(int height, QVector<double> onceAllXDatasVec);
	QVector<double> getOnceAllXDataWithHeight(int height);
	QVector<double> getOnceAllYDataWithHeight(int height);

	//void setAverageDataMap(int height, QVector<double> averageDatasVec);
	//QVector<double> getAverageDataWithHeight(int height);

	QVector<double> getConstXData();

	double * getDataPtrWithIndex(int index);

	//抽取最新的一片内存的数据去显示
	QVector<double> getVecYDatasWithIndex(int index);
	//QVector<double> getVecXDatasWithIndex(int index);

	void updateOnceCollectDatasWithHeight();

	void sortHeightList();
	QList<int> getSortHeightList();
	
private:
	QVector<double> m_constXData;
	//聚焦高度int集合的vector
	QList<int> m_heightList;
	//<z,listItem>
	QMap<int, QListWidgetItem*> m_listwidgetMap;
	//<z,dataMats>  Mat的行列，就是一幅图数据的行列
	QMap<int, QVector<cv::Mat>> m_itemDataMatsMap;
	QMap<int, cv::Mat> m_averageMatMap;
	QMap<int, QVector<QVector3D>> m_vec3DMap;
	cv::Mat m_mat;
	int m_matRow = 0;
	QVector<cv::Mat> m_matVec;
	//静态图对应Map  将Mat的数据 拉成一行
	QMap<int, QVector<double>> m_itemYDataVecMap;
	QVector<double> m_onceYDataAll, m_onceXDataAll;
	double m_xCount = 0;    //x计数
	QMap<int, QVector<double>> m_itemXDataVecMap;
	//每个高度的多幅图经过平均值处理后的单幅图的集合,用于总览项数据的显示
	QMap<int, QVector<double>> m_finalPerData;

	//束腰矩阵
	QMap<int, cv::Mat> m_girldeMatMap;
	QVector<QVector3D> m_girlde3DPoints;

	//采集的数据存储的指针地址
	//QVector<double *> m_dataPtrVec;
	//double * m_pFirstData;
	QVector<double> m_pciDataVec;

	AsThread *m_pCoreDataThread;

	//bool flag = false;
	bool m_isTriggerByHardware = false;
	int m_numberOfDataMat = 1;
	int m_currentHeight;

	QMutex m_mutex;
};

AS_DECLARE_META_TYPE(CoreData)